package com.example.liunanxin.lnx226ordering.model;



import com.example.liunanxin.lnx226ordering.beans.FoodBean;
import com.example.liunanxin.lnx226ordering.iface.SearchListener;
import com.example.liunanxin.lnx226ordering.iface.Searchiface;
import com.example.liunanxin.lnx226ordering.retrofitservice.SearchService;
import com.example.liunanxin.lnx226ordering.url.Url;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SearchModel implements Searchiface {
    private Retrofit retrofit;

    public SearchModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Url.USERURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getFoodBySearch(String search, final SearchListener searchListener) {
        final SearchService searchService=retrofit.create(SearchService.class);
        Call<List<FoodBean>> call=searchService.getFoodBySearch(search);
        call.enqueue(new Callback<List<FoodBean>>() {
            @Override
            public void onResponse(Call<List<FoodBean>> call, Response<List<FoodBean>> response) {
                if(response.isSuccessful() && response!=null)
                    searchListener.onResponse(response.body());
                else
                    searchListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<FoodBean>> call, Throwable t) {
                searchListener.onFail(t.toString());
            }
        });
    }
}
